import { v4 as uuidv4 } from 'uuid';
class UapSDK {
    constructor(elementName,appId,callback){
        this.elementName = elementName;
        this.appId = appId;
        this.timer = null;
        this.serviceUrl = "http://60.204.141.237:8082";
        this.callback = callback;
    }
    createLoginQrcode = function(){
        if(this.timer != null){
            clearInterval(this.timer)
            this.timer = null;
        }
        var uuid = uuidv4().replaceAll("-","");
        console.log(uuid)
        var url = this.serviceUrl+"/api/v1/createLoginQrcode?appId="+this.appId+"&deviceId="+uuid
        var qrcodeDiv = document.getElementById(this.elementName)
        qrcodeDiv.innerHTML = '<div style="text-align:center;font-size:12px;"><img src="'+url+'"></img><div><span id="msg">使用手机APP扫描二维码</span><a id="refresh" style="margin-left:10px;" href="javascript:void(0)">刷新</a></div></div>'
        var refreshBtn = document.getElementById("refresh");
        refreshBtn.addEventListener('click', ()=>{
            this.createLoginQrcode()
        })
        this.timer = setInterval(()=>{
            this.scanStatus(uuid)
        },1000)
    }
    createGrantQrcode = function(){
        if(this.timer!= null){
            clearInterval(this.timer)
            this.timer = null;
        }
        var uuid = uuidv4().replaceAll("-","");
        console.log(uuid)
        var url = this.serviceUrl+"/api/v1/createRegisterQrcode?appId="+this.appId+"&deviceId="+uuid
        var qrcodeDiv = document.getElementById(this.elementName)
        qrcodeDiv.innerHTML = '<div style="text-align:center;font-size:12px;"><img src="'+url+'"></img><div><span id="msg">使用手机APP扫描二维码</span><a id="refresh" style="margin-left:10px;" href="javascript:void(0)">刷新</a></div></div>'
        var refreshBtn = document.getElementById("refresh");
        refreshBtn.addEventListener('click', ()=>{
            this.createGrantQrcode()
        })
        this.timer = setInterval(()=>{
            this.scanStatus(uuid)
        },1000)
    }
    scanStatus = function(deviceId){
        const xhr = new XMLHttpRequest();
        xhr.open('GET', this.serviceUrl+'/api/v1/scanStatus?deviceId='+deviceId);
        xhr.onload = () => {
            if (xhr.status === 200) {
                console.log(xhr.responseText);
                var res = JSON.parse(xhr.responseText);
                var msg = document.getElementById("msg");
                if(res.data.status==0){
                    msg.innerHTML = "二维码已过期"
                    if(this.timer!= null){
                        clearInterval(this.timer)
                        this.timer = null;
                    }
                }else if(res.data.status==1){
                    msg.innerHTML = "使用手机APP扫描二维码"
                }else if(res.data.status==2){
                    msg.innerHTML = "已扫码，等待授权确认"
                }else if(res.data.status==3){
                    msg.innerHTML = "已授权，跳转中..."
                    if(this.timer!= null){
                        clearInterval(this.timer)
                        this.timer = null;
                    }
                    var code = res.data.code;
                    this.callback(code);
                }
            } else {
                console.error(`Error: ${xhr.status}`);
            }
        };
        xhr.send();
    }
}
export {UapSDK} 